package cn.zurish.cloud.snow.tools;

import com.alibaba.excel.annotation.ExcelProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import java.util.List;

import static cn.zurish.cloud.snow.tools.EntityToExcelConverter.convertToExcel;

// BQ 类
class BQ {
    private String externalId;
    private String productName;
    private List<Material> materials;
    private List<Parameter> parameters;

    // Getter 和 Setter 方法
    public String getExternalId() {
        return externalId;
    }

    public void setExternalId(String externalId) {
        this.externalId = externalId;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public List<Material> getMaterials() {
        return materials;
    }

    public void setMaterials(List<Material> materials) {
        this.materials = materials;
    }

    public List<Parameter> getParameters() {
        return parameters;
    }

    public void setParameters(List<Parameter> parameters) {
        this.parameters = parameters;
    }

    @Override
    public String toString() {
        return "BQ{" +
                "externalId='" + externalId + '\'' +
                ", productName='" + productName + '\'' +
                ", materials=" + materials +
                ", parameters=" + parameters +
                '}';
    }
}

// Material 类
class Material {
    private String materialName;

    private String materialBatch;

    private String materialCode;

    private String rawName;

    // Getter和Setter方法
    public String getMaterialName() {
        return materialName;
    }

    public void setMaterialName(String materialName) {
        this.materialName = materialName;
    }

    public String getMaterialBatch() {
        return materialBatch;
    }

    public void setMaterialBatch(String materialBatch) {
        this.materialBatch = materialBatch;
    }

    public String getMaterialCode() {
        return materialCode;
    }

    public void setMaterialCode(String materialCode) {
        this.materialCode = materialCode;
    }

    public String getRawName() {
        return rawName;
    }

    public void setRawName(String rawName) {
        this.rawName = rawName;
    }

    @Override
    public String toString() {
        return "Material{" +
                "rawName='" + rawName + '\'' +
                ", materialCode='" + materialCode + '\'' +
                ", materialName='" + materialName + '\'' +
                ", materialBatch='" + materialBatch + '\'' +
                '}';
    }
}

// Parameter 类
class Parameter {
    private String techKey;

    private String techValue;

    // Getter和Setter方法
    public String getTechKey() {
        return techKey;
    }

    public void setTechKey(String techKey) {
        this.techKey = techKey;
    }

    public String getTechValue() {
        return techValue;
    }

    public void setTechValue(String techValue) {
        this.techValue = techValue;
    }

    @Override
    public String toString() {
        return "Parameter{" +
                "techKey='" + techKey + '\'' +
                ", techValue='" + techValue + '\'' +
                '}';
    }
}


class BQLong {
    @ExcelProperty("工艺卡ID")
    private String externalId;
    @ExcelProperty("物料名称")
    private String productName;
    @ExcelProperty("温控")
    private String temperature;
    @ExcelProperty("齿头")
    private String toothHead;
    @ExcelProperty("单宽")
    private String singleWidth;
    @ExcelProperty("单厚")
    private String singleThickness;

    // Getter 方法
    public String getExternalId() {
        return externalId;
    }

    public String getProductName() {
        return productName;
    }

    public String getTemperature() {
        return temperature;
    }

    public String getToothHead() {
        return toothHead;
    }

    public String getSingleWidth() {
        return singleWidth;
    }

    public String getSingleThickness() {
        return singleThickness;
    }

    // Setter 方法
    public void setExternalId(String externalId) {
        this.externalId = externalId;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public void setTemperature(String temperature) {
        this.temperature = temperature;
    }

    public void setToothHead(String toothHead) {
        this.toothHead = toothHead;
    }

    public void setSingleWidth(String singleWidth) {
        this.singleWidth = singleWidth;
    }

    public void setSingleThickness(String singleThickness) {
        this.singleThickness = singleThickness;
    }
}
public class JsonParserExample {
    public static void main(String[] args) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            // 读取 JSON 文件
            File jsonFile = new File("D:\\Code\\snow\\src\\main\\resources\\data\\data.json");
            // 将 JSON 数组解析为 BQ 列表
            List<BQ> bqList = objectMapper.readValue(jsonFile, objectMapper.getTypeFactory().constructCollectionType(List.class, BQ.class));

            // 打印解析结果
            for (BQ bq : bqList) {
               // System.out.println(bq.getParameters());
                System.out.println(bq);
            }


            List<BQLong> list = bqList.stream().map(bq -> {
                BQLong bqLong = new BQLong();
                bqLong.setExternalId(bq.getExternalId());
                bqLong.setProductName(bq.getProductName());
                bqLong.setTemperature(bq.getParameters().stream().filter(parameter -> parameter.getTechKey().equals("温控")).findFirst().get().getTechValue());
                bqLong.setToothHead(bq.getParameters().stream().filter(parameter -> parameter.getTechKey().equals("齿头")).findFirst().get().getTechValue());
                bqLong.setSingleWidth(bq.getParameters().stream().filter(parameter -> parameter.getTechKey().equals("单宽")).findFirst().get().getTechValue());
                bqLong.setSingleThickness(bq.getParameters().stream().filter(parameter -> parameter.getTechKey().equals("单厚")).findFirst().get().getTechValue());
                return bqLong;
            }).toList();

            // 打印解析结果
            for (BQ bq : bqList) {
                System.out.println(bq);
            }

            // 生成Excel文件
            String excelFilePath = "data.xlsx";
            convertToExcel(list, excelFilePath);

            System.out.println("实体类数据已成功转换为Excel文件：" + excelFilePath);
        } catch (IOException e) {
            System.err.println("转换过程中发生错误：" + e.getMessage());
            e.printStackTrace();
        }
    }
}